import json
import os
import torch
from flask import current_app
from transformers import BertTokenizer, BertConfig

class Config:
    def __init__(self):
        # 初始化参数
        # 1、设备相关
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        # 2、目录路径
        # data目录路径
        self.data_path = os.path.join(current_app.root_path, "data")
        self.save_model_path = os.path.join(current_app.root_path, "save_models")
        self.origin_path = os.path.join(current_app.root_path, "data_origin")

        # 文件路径
        self.tag2id_path = os.path.join(self.data_path, "tag2id.json")
        self.label_path = os.path.join(self.data_path, "labels.json")
        self.train_txt_path = os.path.join(self.data_path, "train.txt")
        self.vocab_txt_path = os.path.join(self.data_path, "vocab.txt")

        # 配置模型相关信息
        self.model_name = "BiLSTM_CRF"

        # 3、bilstm+crf模型相关
        self.embedding_dim = 300
        self.hidden_dim = 256
        self.dropout = 0.2
        self.lr = 2e-3
        self.epochs = 30
        self.batch_size = 16
        self.crf_lr = 1e-3

        # 关系抽取相关配置
        # 输入层
        self.bert_dim = 768
        # 关系类别数
        self.num_rel = 18

        # 数据集路径
        self.bert_base_path = os.path.join(current_app.root_path, "bert-base-chinese")
        # 分词器
        self.tokenizer = BertTokenizer.from_pretrained(self.bert_base_path)
        # bert配置文件
        self.bert_config = BertConfig.from_pretrained(f"{self.bert_base_path}\\bert_config.json")
        # entertainment目录路径
        self.entertainment_data_path = os.path.join(current_app.root_path, "data", "entertainment")
        # 训练集数据路径
        self.bert_train_path = os.path.join(self.entertainment_data_path, "train.json")
        # 验证集数据路径
        self.bert_dev_path = os.path.join(self.entertainment_data_path, "dev.json")
        # 测试集数据路径
        self.bert_test_path = os.path.join(self.entertainment_data_path, "test.json")
        # 标签路径
        self.bert_rel_path = os.path.join(self.entertainment_data_path, "relation.json")
        # 关系类别相关：映射字典、类别数
        self.id2rel = json.load(open(self.bert_rel_path, encoding="utf-8"))
        self.rel2id = {v: int(k) for k, v in self.id2rel.items()}
        # 模型训练相关超参数
        self.bert_epochs = 10
        self.bert_batch_size = 16
        self.bert_learning_rate = 1e-5
        self.rel_size = len(self.rel2id)


